<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
//
require_once 'insertNewFunction.php';

$db = new dbConn();
//定义表名
define("TABLENAME","coupon");

$time = $_GET['time'];
//操作
if(!empty($_GET['id'])){
	$act = "edit";
	$id = $_GET['id'];
}
//提交处理
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$title = $_POST["title"];
	$cost = $_POST["cost"];
	$activity_id = $_POST['activity_id'];
	$seller_id = $_POST["seller_id"];
	$pic = $_POST['pic'];
	$db->sql="INSERT INTO coupon(pic,title,cost,publishDate,activity_id,seller_id) VALUES";
	foreach ($pic as $key=>$item){
		//echo $title[$key];
		$values .= "('".$pic[$key]."','".$title[$key]."',".$cost[$key].",'".$time."','".$activity_id[$key]."','".$seller_id[$key]."'),";
	}
	$db->sql .= rtrim($values,',');
	$db->doExecute();
}

/* ------------begin 文件处理---------------  */
//读取文件夹
$files = read_dir('../content/coupon/');
//读取数据库
$db->sql = "select * from ".TABLENAME." where publishDate >= '".date("Y-m-d")."'";
$files_find = $db->doResultList2();
foreach ($files_find as $item){
	//=====需更改位置======//
	if(PARENTDIR=="content/"){
		$dateKey = explode('-', $item['publishDate']);
		$dateKey = $dateKey[0].$dateKey[1].$dateKey[2];
	}else{$dateKey=$item['publishDate'];}
	$files_find2[$dateKey][] = $item['pic'];
}

$result = getNew($files[0], $files_find2);
$picNew = $result[0];
$gtDay  = $result[1];
if(!empty($picNew)){
	$picNew2 = getName($picNew,$files[1]);
	$picNew2 = getNewContent($picNew2,$files[2]);
}/*------------ end 文件处理  ----------------*/


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	$("form").submit(function(){
		var textinput = $(this).find(".text-input");
		var nullNum = 0;
		$(textinput).each(function() {
			if( $(this).val()==''){nullNum++;}  
        });
		if ( nullNum!=0 ) {
			alert("有"+nullNum+"个输入框为空！不行");
			return false;
		}
	});
	$(".tiqu").click(function(){
		var url = $(this).siblings(".url").val();
		if(url){
			//var re = /activityId=([0-9]+)($|&)/i; // 创建正则表达式模式。  
			var activity_id = tiqu(url ,/activityId=([0-9]+)($|&)/i);
			var seller_id = tiqu(url ,/sellerId=([0-9]+)($|&)/i);
			$(this).siblings(".activity_id").val(activity_id);;
			$(this).siblings(".seller_id").val(seller_id);;  
		}else{
			alert("链接为空！！");
		}
	});
});
function tiqu(s,re){
	//var s = "http://taoquan.taobao.com/coupon/combo/between.htm?spm=9210.1000627.1.2.EyUN0E&activityId=26448135&sellerId=383261993&shopTitle=%B8%BB%B0%D8%BE%C5%B0%D8%D7%A8%C2%F4%B5%EA&discount=5000";  
	//var re = /activityId=([0-9]+)($|&)/i; // 创建正则表达式模式。  
	var r = s.match(re); // 尝试匹配搜索字符串。  
	return (r[1]); // 返回第一次出现 "ain" 的地方。 
}
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">优惠券<?php if($act=="edit") echo "修改";else echo "添加";?>&nbsp;&nbsp;&nbsp;<span style="color: red;">上传文件目录示例content/coupon/日期（必须大于等于当前日期）/***.jpg(.png、……)</span></p>

			<div class="clear"></div>
			<!-- End .clear -->

			<?php //if(count($picNew2)>0):?>
			<?php //foreach ($picNew2 as $key=>$item):?>
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3><?php //echo $key;?>添加优惠券</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
						<form action="?time=<?php echo $key;?>" method="post">
							<fieldset>
								<!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
								
								<p>
									<a href="" title="" target="_blank">
										<img src="" alt="" width="50px;" height="50px;"/>
									</a> 
									<input type="hidden" id="" name="pic[]" value="<?php echo $item2['url'];?>"/>
									<span style="margin-left: 50px;">
										标题：<input class="text-input" type="text" name="title[]" style="width: 250px;"/>  &nbsp;
										需要积分：<input class="text-input" type="text" name="cost[]" style="width: 50px;"/>
									</span>
									<br />
									<small>请填写地址！！</small>
									<input class="text-input large-input url" type="text" name="url[]" value="<?php echo $item2['content'];?>"/><br />
									<small>提取的  activity_id  || seller_id！！请确认。</small><button class="tiqu" type="button">提取</button><br />
									activity_id：<input class="text-input activity_id" type="text" name="activity_id[]" style="width: 150px;background:#D5D5D5;" readonly="readonly"/>
									seller_id：<input class="text-input seller_id" type="text" name="seller_id[]" style="width: 150px;background:#D5D5D5;" readonly="readonly"/>
									<!-- updater originalPrice currentPrice title desc pic goodsUrl -->
								</p>
								
								<p><input class="button" type="submit" value="添加" id="Add<?php echo $key;?>"/></p>
							</fieldset>
							<div class="clear"></div>
							<!-- End .clear -->
						</form>
					</div>
					<!-- End #tab1 -->
				</div>
				<!-- End .content-box-content -->
			</div>
			<?php //endforeach;else:?>
			<!--  <div class="content-box"><div class="content-box-content" style="font-size: x-large;font-weight: bold;">没有新图</div></div>
			--><?php //endif;?>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
